package cn.wwk.canal.publish;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Component
@Slf4j
public class CanalPublisher {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String sendMessage){
        //创建消息
        Message message = MessageBuilder.withBody(sendMessage.getBytes(StandardCharsets.UTF_8))
                .setDeliveryMode(MessageDeliveryMode.PERSISTENT)
                .build();
        //消息Id,需要封装到CorrelationDate中
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        //发送消息
        rabbitTemplate.convertAndSend("canal_exchange", "canal_key", message, correlationData);
        //记录日志
        log.debug("发送消息成功!");
    }

}
